package itcast.dao.impl;

import itcast.dao.RepairDao;
import itcast.domain.Build;
import itcast.domain.Repair;
import itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class RepairDaoImpl implements RepairDao {
    //实现template对象
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    //实现在repair表中添加数据

   /* @Override
    public void addrepair(Repair repair) {
        String sql="select student_id from student where student_username=?";
        String sql1="select drom_id from drom where  drom_name=?";
        String sql2="Select build_id from build_name=?";
        int sq=template.queryForObject(sql,int.class,repair.getStudent_username());
        int sq1=template.queryForObject(sql1,int.class,repair.getDrom_name());
        int sq2=template.queryForObject(sql2,int.class,repair.getBuild_name());
        String sql3="insert into repair(repair_remark,repair_studentid,repair_date) values(?,sq,?)";
        template.update(sql,repair.getRepair_remark(),repair.getRepair_date());
    }*/
   //添加数据

    @Override
    public void addrepair(String repair_date, String student_username, String student_name, String build_name, String drom_name, String repair_remark) {

        String sql="select student_id from student where student_username=?";

     int sq=template.queryForObject(sql,Integer.class,student_username);
        System.out.println(sq);



        String sql3="insert into repair(repair_studentid ,repair_remark,repair_date) values(?,?,?)";

        template.update(sql3,sq,repair_remark,repair_date);

    }
    //在数据库中查找维修数据

    @Override
    public List<Repair> findrepair() {
        String sql="select repair_id,repair_date,repair_remark,student_username,student_name," +
                "drom_type,build_name,drom_name from repair,student,drom,build " +
                "where repair.repair_studentid=student.student_id and student.student_domitoryid=" +
                "drom.drom_id and drom.drom_buildid=build.build_id";
        //query返回list集合，BeanPropertyRowMapper指定字节码
        List<Repair> repairs=template.query(sql,new BeanPropertyRowMapper<Repair>(Repair.class));
        return repairs;
    }
    //学生根据学号查找自身数据

    @Override
    public List<Repair> findRepairStudent_username(String student_username) {
        System.out.println(student_username);
        String sql="select repair_id,repair_date,repair_remark,student_username,student_name," +
                "drom_type,build_name,drom_name from repair,student,drom,build " +
                "where repair.repair_studentid=student.student_id and student.student_domitoryid=" +
                "drom.drom_id and drom.drom_buildid=build.build_id and student_username=?";
        List<Repair> repairs= template.query(sql,new BeanPropertyRowMapper<Repair>(Repair.class),student_username);
        return repairs;
    }
    //根据报修id删除报修信息

    @Override
    public void delrepair(int repair_id) {
        String sql="delete from repair where repair_id=?";
        template.update(sql,repair_id);
    }
}
